#ifndef GRID_H_
#define GRID_H_

#include "RayTraceable.h"
#include "BBox.h"
#include "Scene.h"
#include "Debug.h"

struct CellCache
{
    CellCache(Renderable* object) : m_object(object), m_next(NULL) {}
    Renderable* m_object;
    CellCache* m_next;
};

class Grid : public RayTraceable
{
public:
    Grid(Scene& scene, vRenderables& renderables) : 
      m_scene(scene),
      m_renderables(renderables), m_x(0), m_y(0), m_z(0) {}
    void SetUpCells(const int x = 0,const int y = 0,const int z = 0);
    void CleanUp();
    virtual bool Hit(const Ray& ray, float& tmin, ContactData& data) const;
    const BBox& GetBox() const;

#ifdef DEBUGGING
        bool Hit_Debug(const Ray& ray, float& tmin, ContactData& data) const;
#endif

private:
    std::vector<CellCache*> m_cells;
    BBox m_box;
    int m_x, m_y, m_z;

    Vec3 Calculate_Min();
    Vec3 Calculate_Max();
    float Clamp(float x, float min, float max) const {return x < min ? min : ( x > max ? max : x);}
    Scene& m_scene;
    vRenderables& m_renderables;
};

#endif